﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace VioletDevelopmentLaboratory.Net.Web.Component
{
	public abstract class BoardViewPartBase : BoardTemplateBase
	{
		public virtual void AccessValidate()
		{
			Board CurrentBoard = (Board)this.Page.Items["CurrentBoard"];
			VBoardUserInfo CurrentUserInfo = (VBoardUserInfo)this.Page.Items["CurrentUserInfo"];
			BoardThread CurrentThread = (BoardThread)this.Page.Items["CurrentThread"];

			//권한 검사
			//글보기 권한이 있는지 확인
			if (!(CurrentUserInfo.IsAdministrator ||
				CurrentUserInfo.IsSysop(CurrentBoard) ||
				CurrentUserInfo.IsAuthorizedBy(CurrentBoard, BoardCommand.Read)))
			{
				//글보기 권한 없음
				VUtility.GoToMessagePage(Page, VUtility.GetVWebGlobalResource("Board_Do_ThreadView_Rejection"),
					BoardUtil.GetBoardUrl(BoardPart.List, BoardParameters.PageIndex, BoardParameters.CategoryID, "", BoardParameters.SearchText, 0, 0), true, 3);
			}

			//존재하지 않는 글
			if (CurrentThread == null)
				VUtility.GoToMessagePage(Page, VUtility.GetVWebGlobalResource("Board_Do_ThreadView_InvalidID"),
					BoardUtil.GetBoardUrl(BoardPart.List, BoardParameters.PageIndex, BoardParameters.CategoryID, "", BoardParameters.SearchText, 0, 0), true, 3);

			if (CurrentThread.IsSecret)
			{
				Boolean doViewSecret = CurrentUserInfo.IsAdministrator || CurrentUserInfo.IsSysop(CurrentBoard) || (CurrentThread.ProviderUserKey == CurrentUserInfo.ProviderUserKey);

				//비밀글보기 권한 없음
				if (!doViewSecret)
					VUtility.GoToMessagePage(Page, VUtility.GetVWebGlobalResource("Board_Do_ThreadView_RejectionSecret"),
						BoardUtil.GetBoardUrl(BoardPart.List, BoardParameters.PageIndex, BoardParameters.CategoryID, "", BoardParameters.SearchText, 0, 0), true, 3);
			}
		}

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			AccessValidate();
		}
	}
}
